package com.example.myapplicationgd;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelAdapter;

public class SlotMachineActivity extends Activity {
	private int doneIndex = 0;

    String oldKjh = "5,9,10,2,5";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.slot_machine_layout);


        String[] split = oldKjh.split(",");
        //初始化
        initWheel(R.id.slot_1,Integer.parseInt(split[0]));
        initWheel(R.id.slot_2,Integer.parseInt(split[1]));
        initWheel(R.id.slot_3,Integer.parseInt(split[2]));
        initWheel(R.id.slot_4,Integer.parseInt(split[3]));
        initWheel(R.id.slot_5,Integer.parseInt(split[4]));

        Button mix = (Button)findViewById(R.id.btn_mix);



        mix.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(doneIndex==0){
                    doneIndex = 1;
                    String newKjh = "2,5,8,10,3";

                    mixWheel(R.id.slot_1,posIndex(oldKjh,newKjh,0),3000);
                    mixWheel(R.id.slot_2,posIndex(oldKjh,newKjh,1),4000);
                    mixWheel(R.id.slot_3,posIndex(oldKjh,newKjh,2),5000);
                    mixWheel(R.id.slot_4,posIndex(oldKjh,newKjh,3),6000);
                    mixWheel(R.id.slot_5,posIndex(oldKjh,newKjh,4),7000);
                    oldKjh = newKjh;
                }

            }
        });

    }

    private int posIndex(String oldKjh,String newKjh,int index){
        String[] oldsplit = oldKjh.split(",");
        String[] newsplit = newKjh.split(",");
        int oldOneValue = Integer.parseInt(oldsplit[index]);
        int newOneValue = Integer.parseInt(newsplit[index]);
        int a = oldOneValue - 1;
        int b = newOneValue - 1;
        return a-b;
    }
    
    // 车轮滚动标志
    private boolean wheelScrolled = false;
    
    //车轮滚动的监听器
    OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
        public void onScrollingStarted(WheelView wheel) {
            wheelScrolled = true;
        }
        public void onScrollingFinished(WheelView wheel) {
        	wheelScrolled = false;
            doneIndex = doneIndex+1;
            System.out.println("轮子---->"+wheel.getCurrentItem()+"-->doneIndex:"+doneIndex);

            if(doneIndex==6){
                doneIndex = 0;
            }


        }
    };
    
    // 车轮item改变的监听器
    private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (!wheelScrolled) {
            	System.out.println("轮子item---->"+wheel.getCurrentItem());

            }
        }
    };
    

    /**
     * 初始化轮子
     * @param id the wheel widget Id
     */
    private void initWheel(int id) {
        WheelView wheel = getWheel(id);
        wheel.setViewAdapter(new SlotMachineAdapter(this));
        //wheel.setCurrentItem((int)(Math.random() * 10));
        wheel.setCurrentItem(0);
        wheel.addChangingListener(changedListener);
        wheel.addScrollingListener(scrolledListener);
        wheel.setCyclic(true);
        wheel.setEnabled(false);
    }

    /**
     * 初始化轮子
     * @param id the wheel widget Id
     */
    private void initWheel(int id,int value) {
        WheelView wheel = getWheel(id);
        wheel.setViewAdapter(new SlotMachineAdapter(this));
        //wheel.setCurrentItem((int)(Math.random() * 10));
        wheel.setCurrentItem(value-1);
        wheel.addChangingListener(changedListener);
        wheel.addScrollingListener(scrolledListener);
        wheel.setCyclic(true);
        wheel.setEnabled(false);
    }
    
    /**
     * 根据id获取轮子
     * @param id the wheel Id
     * @return the wheel with passed Id
     */
    private WheelView getWheel(int id) {
        return (WheelView) findViewById(id);
    }
    
    /**
     * 测试轮子转动位置
     * @return true 
     */
    private boolean test() {
        int value = getWheel(R.id.slot_1).getCurrentItem();
        return testWheelValue(R.id.slot_2, value) && testWheelValue(R.id.slot_3, value);
    }
    
    /**
     * 根据轮子id获取当前item值
     * @param id the wheel Id
     * @param value the value to test
     * @return true if wheel value is equal to passed value
     */
    private boolean testWheelValue(int id, int value) {
        return getWheel(id).getCurrentItem() == value;
    }
    
    /**
     * 转动轮子
     * @param id the wheel id
     */
    private void mixWheel(int id,int round,int time) {
        WheelView wheel = getWheel(id);
        round = round + 11;

        wheel.scroll(round, time);
        //wheel.scroll((int)(Math.random() * 50)+round, time);
        //wheel.scroll(-350 + (int)(Math.random() * 50), 2000);
    }
    
    /**
     * 老虎机适配器
     */
    private class SlotMachineAdapter extends AbstractWheelAdapter {
        // 图片的大小
        final int IMAGE_WIDTH = 150;
        final int IMAGE_HEIGHT = 150;
        
        // 图片的数组
        private final int items[] = new int[] {
        		R.mipmap.ball_1,
        		R.mipmap.ball_2,
        		R.mipmap.ball_3,
        		R.mipmap.ball_4,
                R.mipmap.ball_5,
                R.mipmap.ball_6,
                R.mipmap.ball_7,
                R.mipmap.ball_8,
                R.mipmap.ball_9,
                R.mipmap.ball_10,
                R.mipmap.ball_11,
        };
        
        // 对图片的缓存
        private List<SoftReference<Bitmap>> images;
        
        // 布局膨胀器
        private Context context;
        
        /**
         * 构造函数
         */
        public SlotMachineAdapter(Context context) {
            this.context = context;
            images = new ArrayList<SoftReference<Bitmap>>(items.length);



            for (int id : items) {
                images.add(new SoftReference<Bitmap>(loadImage(id)));
            }

        }
        
        /**
         * 从资源加载图片
         */
        private Bitmap loadImage(int id) {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),id);
            Bitmap scaled = Bitmap.createScaledBitmap(bitmap, IMAGE_WIDTH, IMAGE_HEIGHT, true);
            bitmap.recycle();
            return scaled;
        }

        @Override
        public int getItemsCount() {
            return items.length;
        }

        // 设置图片布局的参数
        final LayoutParams params = new LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT);
        
        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            ImageView img;
            if (cachedView != null) {
                img = (ImageView) cachedView;
            } else {
                img = new ImageView(context);
            }
            img.setLayoutParams(params);
            SoftReference<Bitmap> bitmapRef = images.get(index);
            Bitmap bitmap = bitmapRef.get();
            if (bitmap == null) {
                bitmap = loadImage(items[index]);

                images.set(index, new SoftReference<Bitmap>(bitmap));
            }
            img.setImageBitmap(bitmap);
            img.setPadding(0,10 ,0,10);
            return img;
        }
    }
}
